package com.solution.admin.area.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.area.entity.AreaEntity;
import com.solution.admin.area.service.AreaService;
  
/**
 * 区域信息控制层
 * 
 * @author diwankang
 * @date 2017年4月10日 上午10:55:21
 */
@RequestMapping("/area")
@Controller
public class AreaController {

	private Logger logger = LoggerFactory.getLogger(AreaController.class);

	@Autowired
	private AreaService areaService;
	
	/**
	 * @param parentId 父节点
	 * @param rootDepth 节点深度  1 省 2 市 3区  4街道
	 * @return
	 * @author: solin
	 * @date: 2017年5月5日 下午2:32:20
	 */
	@RequestMapping("/getAreaList")
	@ResponseBody
    public List<AreaEntity> getAreaList(String parentId, String rootDepth) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("parentId", parentId);
		paramMap.put("rootDepth", rootDepth);
		List<AreaEntity> areaList = null; 
		try {
			areaList = areaService.listArea(paramMap); 
		} catch (Exception e) {
			logger.error("查询区域列表失败", e);
		}
		return areaList;
    }
	
	
	/**
	 * 跳转区域选择器
	 * @author james
	 */
	@RequestMapping("/toAreaSelector")
    public String toAreaSelector(String parentId, String rootDepth) {
		return "/coupon/area_selector";
    }
	
	
	/**
	 * 一次性加载区域级别的数据
	 * @author james
	 */
	@RequestMapping("/listAreaListForSelector")
	@ResponseBody
    public List<AreaEntity> listAreaListForSelector(HttpServletRequest request) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("parentId", request.getParameter("id"));
		List<AreaEntity> areaList = areaService.listAreaForSelector(param);
		if(areaList != null && areaList.size() > 0){
			for (AreaEntity area : areaList) {
				if(area.getPath().length() <= 15){
					area.setIsParent("true");
				}else{
					area.setIsParent("false");
				}
			}
		}
		return areaList;
    }
	
	

}
